package com.ly.monogoDB;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;

	
public class MyMonogoDb {
	public static final String uuid = "_uuid";
	
	/**
	 * 创建数据库
	 * @param path 配置文件路径
	 * @throws Exception 
	 */
	public void createData(String path) throws Exception{
		FileOutputStream file = new FileOutputStream(path);
		Document document = new Document();
		Element root = new Element("database");
		Element tag = new Element("table");
		tag.setAttribute("name","test");
		Element tag2 = new Element("tables");
		tag2.setAttribute("name", "system.indexs");
		Element tag3 = new Element("table");
		tag3.setAttribute("name","system.users");
		
		root.setContent(tag);
		root.setContent(tag2);
		root.setContent(tag3);
		document.setRootElement(root);
		System.out.println(root);
		XMLOutputter out = new XMLOutputter();
		out.output(document, file);
		System.out
				.println("----------create---database---success-------------------");
	}
	/**
	 * 插入数据
	 * @throws FileNotFoundException 
	 */
	public boolean addData(String path ,String tableName ,Object obj ) throws Exception{
		Gson gson = new Gson();
		boolean flag = false;
		FileInputStream file = new FileInputStream(path);
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(file);
		Element root = document.getRootElement();
		List<Element> list = root.getChildren();
		for(Element el:list){
			if(el.getAttributeValue("name").equals(tableName)){
				Map base = new HashMap();
				base.put(uuid, UUID.randomUUID().toString().replace("-", ""));
				Element data = new Element("data");
				String json =gson.toJson(obj);
				Map map = gson.fromJson(json, Map.class);
				base.putAll(map);
				data.addContent(gson.toJson(base));
				el.addContent(data); 
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream(path));
		flag=true;
		System.out.println("插入数据成功");
		return flag;
		
		
	}

	/**
	 *  获取表全部数据
	 * @throws Exception 
	 */
	public List<Map> findAll(String path ,String tableName) throws Exception{
		Gson gson = new Gson();
		FileInputStream file = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		
		List<Map> list = new ArrayList();
		List<Element> elist = root.getChildren();
		
		for(Element e:elist){
			String name = e.getAttributeValue("name");
			if(name.equals(tableName)){
				List<Element> l =e.getChildren();
				for (Element element : l) {
					String data = element.getText();
					if(data==null)break;
					Map map = gson.fromJson(data,Map.class);
					list.add(map);
				} 
			}
		}
		return list;
	}
	
	
	/**
	 * 表修改数据
	 * 
	 * @param path
	 * @param tablename
	 * @param po
	 * @throws JDOMException
	 * @throws IOException
	 */
	public void updateData(String path, String tablename, Object po)
			throws JDOMException, IOException {
		FileInputStream file = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		Gson gson = new Gson();

		List<Element> list = root.getChildren();
		for (Element x : list) {
			if (x.getAttributeValue("name").equals(tablename)) {
				List<Element> al = x.getChildren();
				for (Element s : al) {
					// 如果定位Data[通过UUID唯一标示]
					Map mp = gson.fromJson(s.getText(), Map.class);
					Map newmp = gson.fromJson(gson.toJson(po), Map.class);
					if (mp.get(uuid).equals(newmp.get(uuid))) {
						mp.putAll(newmp);
						Element data = new Element("data");
						data.setText(gson.toJson(mp));
						x.removeContent(s);
						x.addContent(data);
						break;
					}
				}
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream(path));
		System.out
		.println("----------update --- data --- success----------------------");
	}

	/**
	 * 表删除数据
	 * 
	 * @param path
	 * @param tablename
	 * @param po
	 * @throws JDOMException
	 * @throws IOException
	 */
	public void deleteData(String path, String tablename, Object po)
			throws JDOMException, IOException {
		FileInputStream file = new FileInputStream(path);
		Gson gson = new Gson();
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		List<Element> list = root.getChildren();
		for (Element x : list) {
			List<Element> al = x.getChildren();
			if (x.getAttributeValue("name").equals(tablename)) {
				for (Element s : al) {
					// 如果定位Data[通过UUID唯一标示]
					Map mp = gson.fromJson(s.getText(), Map.class);
					Map newmp = gson.fromJson(gson.toJson(po), Map.class);
					if (mp.get(uuid).equals(newmp.get(uuid))) {
						x.removeContent(s);
						break;
					}
				}
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream(path));
		System.out
		.println("----------delete --- data --- success----------------------");
	}
}
